/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mx.netbrain.bean;

import java.io.File;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import mx.netbrain.dao.StorageDao;
import mx.netbrain.dao.impl.StorageDaoImpl;
import mx.netbrain.model.Status;
import mx.netbrain.model.Storage;
import mx.netbrain.util.CreateFolderUtil;
/**
 *
 * @author gamma
 */
@ManagedBean(name = "storageBean")
@SessionScoped
public class StorageBean {
    private Storage storage = new Storage();
    private Status status = new Status();
    private String nameVol;
    private String routeVol;
    private String physicalroute;
    private static char sep = File.separatorChar;
    private BigDecimal spaceVol;
    private List<Storage> listStorage;
    
    public StorageBean(){
    }
    
    public void prepareStorage(Integer id){
        StorageDao storageDao = new StorageDaoImpl();
        storage = storageDao.findById(id);
    }
    
    public void saveVol(){
        StorageDao storageDao = new StorageDaoImpl();
        storage.setName(nameVol);
        storage.setRoute(routeVol);
        storage.setSpace(spaceVol);
        status.setId(1);
        storage.setStatus(status);
        storage.setRegister(new Date());
        storageDao.save(storage);
        int character = routeVol.length();
        char car = routeVol.charAt(character - 1);
        if (car != sep){
            physicalroute = routeVol + sep;
        }
        CreateFolderUtil.createFolderUtil(physicalroute
                + storage.getId() + sep);
        FacesContext context = FacesContext.getCurrentInstance();
        context.addMessage(null, new FacesMessage("¡El registro se guardó correctamente!", "Guardado"));
        storage = new Storage();
        nameVol = new String();
        routeVol = new String();
        spaceVol = new BigDecimal(0);
    }
    
    public void deleteVol(){
        StorageDao storageDao = new StorageDaoImpl();
        storageDao.delete(storage);
        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO,"¡Se ha borrado el registro correctamente!", "Actualizado");
        FacesContext.getCurrentInstance().addMessage(null, message);
        storage = new Storage();
    }
    
    public void editVol(){
        StorageDao storageDao = new StorageDaoImpl();
        storageDao.update(storage);
        FacesContext context = FacesContext.getCurrentInstance();
        context.addMessage(null, new FacesMessage("¡Registro actualizado correctamente!", "Actualizado"));
        storage = new Storage();
    }

    public Storage getStorage() {
        return storage;
    }

    public void setStorage(Storage storage) {
        this.storage = storage;
    }

    public Status getStatus() {
        return status;
    }

    public void setStatus(Status status) {
        this.status = status;
    }

    public String getNameVol() {
        return nameVol;
    }

    public void setNameVol(String nameVol) {
        this.nameVol = nameVol;
    }

    public String getRouteVol() {
        return routeVol;
    }

    public void setRouteVol(String routeVol) {
        this.routeVol = routeVol;
    }

    public BigDecimal getSpaceVol() {
        return spaceVol;
    }

    public void setSpaceVol(BigDecimal spaceVol) {
        this.spaceVol = spaceVol;
    }

    public List<Storage> getListStorage() {
        StorageDao storageDao = new StorageDaoImpl();
        listStorage = storageDao.listAll();
        return listStorage;
    }

    public void setListStorage(List<Storage> listStorage) {
        this.listStorage = listStorage;
    }
}
